<?php        
if(!session_id()) { @session_start(); }

	$_SESSION['payfast_post_session'] = json_encode($_POST);

  $root = dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))); 

if (file_exists($root.'/wp-load.php')) {
      
  require_once($root.'/wp-load.php'); 

}   
error_reporting(0);
$plugin_url = plugins_url('',  dirname(__FILE__));
$base =   dirname(dirname(dirname(__FILE__)));

$payfast_merchant_id = get_option("moveto_payfast_merchant_id");
$payfast_marchant_key = get_option("moveto_payfast_marchant_key");
$payfast_testing_mode = get_option("moveto_payfast_testing_mode");

$cartTotal = $_SESSION['final_amt'];

$confirmation_address = $_SESSION['first_step_data']['email'];

$return_url = $plugin_url.'/lib/payfast_payment_success.php';
$cancel_url = get_option("siteurl");
$notify_url = $plugin_url.'/lib/payfast_payment_notify.php';
$pfOutput ="";

$data = array(
	'merchant_id' => $payfast_merchant_id,
	'merchant_key' => $payfast_marchant_key,
	'return_url' => $return_url,
	'cancel_url' => $cancel_url,
	'notify_url' => $notify_url,
	'amount' => number_format( sprintf( "%.2f", $cartTotal ), 2, '.', '' ),
	'item_name' => 'Moveto',
	'email_confirmation' => '1',
	'confirmation_address' => $confirmation_address
);

foreach( $data as $key => $val ){
	if(!empty($val)){
		$pfOutput .= $key .'='. urlencode( trim( $val ) ) .'&';
	}
}

$getString = substr( $pfOutput, 0, -1 );
if( isset( $passPhrase ) ){
	$getString .= '&passphrase='. urlencode( trim( $passPhrase ) );
}

$data['signature'] = md5($getString);
$datasignature = md5($getString);

if($payfast_testing_mode == "E"){
	$payfast_url = "https://sandbox.payfast.co.za/eng/process";
} else {
	$payfast_url = "https://www.payfast.co.za/eng/process";
}

$pf_payment_detail = array();
$pf_payment_detail['post_action'] = $payfast_url;

$pf_Params = array();
$pf_Params['pay_status'] = "pf_proceed_payment";
$pf_Params['merchant_id'] = $payfast_merchant_id;
$pf_Params['return_url'] = $return_url;
$pf_Params['cancel_url'] = $cancel_url;
$pf_Params['notify_url'] = $notify_url;
$pf_Params['merchant_key'] = $payfast_marchant_key;
$pf_Params['amount'] = number_format( sprintf( "%.2f", $cartTotal ), 2, '.', '' );
$pf_Params['item_name'] = 'Moveto';
$pf_Params['email_confirmation'] = "1";
$pf_Params['confirmation_address'] = $confirmation_address;
$pf_Params['signature'] = $datasignature;

$Extra_form_fields = '';
foreach($pf_Params as $name => $value) {
	$Extra_form_fields .= '<input type="hidden" name="' . $name .'" value="' . $value . '">';
}
$pf_payment_detail['Extra_form_fields'] = $Extra_form_fields;

echo json_encode($pf_payment_detail);
?>